package com.itwn.controller;

import com.itwn.entity.Manager;
import com.itwn.result.ResponseResult;
import com.itwn.service.ManagerService;
import com.itwn.vo.ChildrenPermission;
import com.itwn.vo.ParentPermission;
import com.itwn.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin")
@Api("管理员操作")
@CrossOrigin
public class LoginController {
    private ManagerService managerService;

    @Autowired
    public LoginController(ManagerService managerService) {
        this.managerService = managerService;
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType="query",name="userCode",value="登录ID",
                    dataType="String",required=true),
            @ApiImplicitParam(
                    paramType="query",name="userPass",value="登录密码",
                    dataType="String",required=true),
    })
    public ResponseResult<Manager> login(@RequestParam("userCode") String userCode, @RequestParam("userPass")  String userPass){
        Manager manager = managerService.findByCode(userCode);
        ResponseResult<Manager> result = new ResponseResult<>();
        if (manager==null){
            result.setAll(201,"找不到用户",null);
        }else if(!manager.getUserPassword().equals(userPass)){
            result.setAll(202,"密码错误",null);
        }else {
            result.setAll(200,"登录成功",manager);
        }
        //System.out.println(result);
        return result;
    }

    @PostMapping("/queryMyRole")
    @ApiOperation(value = "依照userCode查询管理员角色")
    @ApiImplicitParam(
            paramType="query",name="userCode",value="管理员编号",
            dataType="String",required=true)
    public ResponseResult<List<ParentPermission>> queryMyRole(@RequestParam("userCode") String userCode){
        List<PermissionVo> voList = managerService.findPermissionByCode(userCode);
        ResponseResult<List<ParentPermission>> result = new ResponseResult<>();
        if (voList!=null){
            List<ParentPermission> parentPermissions = new ArrayList<>();
            for (PermissionVo vo : voList) {
                if (vo.getParentId()==-1 && vo.getUrlType()==1){
                    ParentPermission parentPermission = new ParentPermission(vo.getId(),vo.getName(),new ArrayList<ChildrenPermission>());
                    parentPermissions.add(parentPermission);
                }
            }
            for (PermissionVo vo : voList) {
                if (vo.getParentId()!=-1 && vo.getUrlType()==1){
                    ChildrenPermission childrenPermission = new ChildrenPermission(vo.getId(),vo.getName(),vo.getUrl());
                    for (ParentPermission p:parentPermissions){
                        if (vo.getParentId()==p.getId()){
                            p.getChildren().add(childrenPermission);
                        }
                    }
                }
            }
            System.out.println(parentPermissions);
            result.setAll(200,"用户所拥有的权限",parentPermissions);
        }else {
            result.setAll(201,"查询失败",null);
        }
        return result;
    }
}
